package crm.control;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;

import crm.dbcoon.DBConnect;
import crm.model.Week_plan;

public class WeekplanControl {
	/**
	 * 周计划添加
	 */
	public static void addWeekplan(int all_plan_id,int create_person_id) {
		DBConnect dbc = new DBConnect();
		String sql = "insert into week_plan(all_plan_id,date,content,help_person,description,create_person_id) values(?,?,?,?,?,?)";
		dbc.prepareStatement(sql);
		try {
			int index = 1;
			dbc.setInt(index++, all_plan_id);
			dbc.setString(index++, "");
			dbc.setString(index++, "");
			dbc.setString(index++, "");
			dbc.setString(index++, "");
			dbc.setInt(index++, create_person_id);
			dbc.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			dbc.close();
		}
	}
	/**
	 * 获取周计划列表
	 */
	public static Collection<Week_plan> getAllWeepPlans(int allPlanId) {
		Collection<Week_plan> wp = new ArrayList<Week_plan>();
		DBConnect dbc = new DBConnect();
		String sql = "select * from week_plan where all_plan_id = '"+allPlanId+"'";
		dbc.prepareStatement(sql);
		ResultSet rs = null;
		try {
			rs = dbc.executeQuery();
			while (rs.next()) {
				Week_plan weekPlan = new Week_plan();
				weekPlan.setId(rs.getInt("id"));
				weekPlan.setAll_plan_id(rs.getInt("all_plan_id"));
				weekPlan.setDate(rs.getString("date"));
				weekPlan.setContent(rs.getString("content"));
				weekPlan.setHelp_person(rs.getString("help_person"));
				weekPlan.setDescription(rs.getString("description"));
				weekPlan.setCreate_person_id(rs.getInt("create_person_id"));
				wp.add(weekPlan);
			}
			return wp;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}finally{
			dbc.close();
		}
	}
	/**
	 * 
	 * @param id
	 * @return
	 */
	public static Week_plan getWeekPlanByid(int id) {
		DBConnect dbc = new DBConnect();
		Week_plan weekPlan = new Week_plan();
		String sql = "select * from week_plan where id='"+id+"'";
		dbc.prepareStatement(sql);
		ResultSet rs = null;
		try {
			rs=(ResultSet)dbc.executeQuery();
			if(rs.next()){
				weekPlan.setId(rs.getInt("id"));
				weekPlan.setAll_plan_id(rs.getInt("all_plan_id"));
				weekPlan.setDate(rs.getString("date"));
				weekPlan.setContent(rs.getString("content"));
				weekPlan.setDescription(rs.getString("description"));
				weekPlan.setCreate_person_id(rs.getInt("create_person_id"));
			}
			return weekPlan;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("get出错");
			return null;
		}finally{
			dbc.close();
		}
	}
	/**
	 * 
	 * @param weekSum
	 * @param id
	 * @return
	 */
	public static Boolean updateWeekPlan(Week_plan weekPlan,int id) {
		DBConnect dbc = new DBConnect();
		String sql = "update week_plan set date=?,content=?,help_person=?,description=? where id='"+id+"'";
		dbc.prepareStatement(sql);
		try {
			int index = 1;
			dbc.setString(index++, weekPlan.getDate());
			dbc.setString(index++, weekPlan.getContent());
			dbc.setString(index++, weekPlan.getHelp_person());
			dbc.setString(index++, weekPlan.getDescription());
			dbc.executeUpdate();
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}
	}

}
